package cn.com.generaldata.os.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import cn.com.generaldata.os.pojo.Billboard;
import cn.com.generaldata.os.pojo.Unit;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.service.common.BillboardService;

public class AutoPushingJob implements Job, ApplicationContextAware {

	private static ApplicationContext ctx;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		String userIds = context.getJobDetail().getJobDataMap().getString("userIds");
		String billboardTitle = context.getJobDetail().getJobDataMap().getString("billboardTitle");
		String billboardContent = context.getJobDetail().getJobDataMap().getString("billboardContent");
		User operator = (User) context.getJobDetail().getJobDataMap().get("operator");
		String billId = context.getJobDetail().getJobDataMap().getString("billId");
		BillboardService billboardService = (BillboardService) ctx.getBean("billboardService");

		List<Unit> users = new ArrayList<Unit>();
		for (String userId : userIds.split(",")) {
			Unit user = new Unit();
			user.setId(userId.trim());
			users.add(user);
		}

		billboardService.createBillboard(new Billboard(null, operator, billboardTitle, billboardContent, new Date(),
				billId), users);

	}

	@Override
	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		AutoPushingJob.ctx = ctx;
	}

}
